<?php

namespace addons\venue\controller;

use addons\venue\library\MpWechat;
use app\admin\model\venue\message\Message;
use app\admin\model\venue\pact\Pact;
use app\common\controller\Api;
use think\Cache;
use think\Config;

class Common extends Api
{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['list','detail','pact','common'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    protected $getAuthCodeUrl = "https://open.weixin.qq.com/connect/oauth2/authorize";
    public function common(){
//        Cache::tag("venue")->clear();
        $config = get_addon_config("venue");
        $data = [
            "default_points"    =>  $config['default_points'], //默认支付返积分
            "app_name"    =>  $config['app_name'], //小程序名称
            "points"            =>  $config['points'],         //签到获得积分
            "addup"             =>  $config['addup'],          //累计签到递增积分
            "max"               =>  $config['max'],            //连续签到最大天数
            "app_filings"       =>  $config['app_filings'],            //APP备案号
            "mini_filings"      =>  $config['mini_filings'],            //小程序备案号
            "business_license"  =>  $config['business_license'],            //经营许可
            "share_title"  =>  $config['share_title'],            //分享标题
            "share_img"  =>  cdnurl($config['share_img'],true),            //分享图片
            "yhqimg"            =>  cdnurl($config['yhqimg'],true),            //优惠券页面图片
            'mpurl' => (new MpWechat)->getAuthorizeUrl(),
            "fz_h5_title"       =>  $config["fz_h5_title"],          //公众号分享标题
            "fz_h5_intro"       =>  $config["fz_h5_intro"],         //公众号分享描述
            "fz_h5_url"         =>  $config["fz_h5_url"],         //公众号分享地址
            "fz_h5_fximg"       =>  cdnurl($config["fz_h5_fximg"],true),          //公众号分享图片
            'auth_type' => $config["auth_type"],
        ];
        $data['message'] = Message::field('message_type,masterplate')->select();
        $this->success("获取成功",$data);
    }
        /**
     * 获取url
     * @return string
     */
    public function getAuthorizeUrl()
    {
        $callback = $this->request->post("callback")??'';
        if($callback == ''){
            $this->error("参数错误");
        }
        $config = get_addon_config('venue');
        $state = md5(uniqid(rand(), true));
        $queryArr = array(
            "appid"         => $config['wechat']['app_id'],
            "redirect_uri"  => $callback,
            "response_type" => "code",
            "scope"         => $config['auth_type'] ? 'snsapi_userinfo' : 'snsapi_base',
            "state"         => $state,
        );
        return $this->getAuthCodeUrl . '?' . http_build_query($queryArr) . '#wechat_redirect';
    }

    /*
     * 获取协议
     */
    public function pact(){
        $pact = $this->request->post("pact");
        if (!$pact){
            $this->error("缺少参数");
        }
        $res = Pact::get(["title"=>$pact]);
        if (!$res){
            $this->error("协议不存在");
        }
        $this->success("成功",$res);

    }


}
